import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Stack;

public class Test {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
//        myQueue.offer(2);
//        myQueue.offer(3);
//        myQueue.offer(4);
        System.out.println(myQueue.peek());
        System.out.println(myQueue.poll());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.poll());
//        System.out.println(myQueue.poll());

        System.out.println(myQueue.getUsedSize());

    }
    public static void main4(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.peek());



    }
    public static void main3(String[] args) {
        MySingleLIst mySingleLIst = new MySingleLIst();
        mySingleLIst.addLast(1);
        mySingleLIst.addLast(2);
        mySingleLIst.addLast(3);
        mySingleLIst.addLast(4);
        mySingleLIst.addLast(5);
        mySingleLIst.display1();

    }
    public static void main2(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);//进栈
        stack.push(7);
        stack.push(6);
        stack.push(8);
        Integer a = stack.pop();//出栈
        System.out.println(a);
        Integer b = stack.peek();//查看
        System.out.println(b);
        Integer b2 = stack.peek();
        System.out.println(b2);
       System.out.println(stack.isEmpty());//查看栈是否为空

    }
    public static void main1(String[] args) {
        //ArrayList<String>  list = new ArrayList<>();
       // LinkedList<String> linkedList = new LinkedList<String>(list);
        LinkedList<String> linkedList = new LinkedList<String>();
        linkedList.add("hello");
        linkedList.add("adv");
        linkedList.add("dfg");
        linkedList.add("geg");
        System.out.println(linkedList);
        for (String x:linkedList) {
            System.out.println(x);
        }
        ListIterator<String> it = linkedList.listIterator();
        while(it.hasNext()){//hasNext表示如果有下一个.就进入循环，将其打印
            System.out.println(it.next());//next表示it向后走一步
        }
        System.out.println("======");
        ListIterator<String> it2 = linkedList.listIterator(linkedList.size());
        while(it2.hasPrevious()){//判断是否有前一个，若有，则进入打印
            System.out.println(it2.previous());//同时向前走一步
        }
    }
    //后缀表达式求值
    public int evalRPN(String[] tokens){
        Stack<Integer> stack = new Stack<>();
        for(String x:tokens){//循环遍历元素
            if(!isOperation(x)){//判断不是运算符
                stack.push(Integer.parseInt(x));//x在遍历的时候定义的String类型，要放入栈中，栈指定的类型是Integer，需要将其转为整数
            }else{//若是运算符
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(x){//判断运算符是那个运算符
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }
            }
        }
        return stack.pop();//当上述运算完之后，直接返回栈中的栈顶元素，因为栈中只剩下运算的结果
    }
    //判断是否为运算符
    private boolean isOperation(String x){
        if(x.equals("+") || x.equals("-") || x.equals("/") || x.equals("*")){
            return true;
        }
        return false;
    }
    //括号匹配
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch =='{' || ch == '[' || ch == '('){//如果都是左括号入栈
                stack.push(ch);
            }else{//如果遇到右括号
                if(stack.empty()){//判断栈空还是不空，若空，则是右括号多
                    return false;
                }
                char ch2 = stack.peek();//若不是空，则通过查看操作来看一下
                if(ch2 == '('&& ch == ')' || ch2 =='{'&& ch == '}'|| ch2 == '[' && ch ==']' ){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack.empty()){//判断栈，若不为空，返回false
            return false;
        }
        return true;//若为空，返回true
    }
    public boolean isPopOrder(int [] pushA,int [] popA ){
        if(pushA == null){
            return false;
        }
        if(popA == null){
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        //通过遍历将pushA的值压入栈中，压一个值，和popA中一个值比较一次
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            while(j < popA.length && !stack.empty() && stack.peek().equals(popA[j])){
                stack.pop();
                j++;
            }
        }
        return stack.empty();//最后检查栈是否为空
    }

}
